// pages/csregistration/csregistration.js
var util = require("../../utils/util.js")
var api = require("../../utils/api.js")
var app = getApp();
const device = wx.getSystemInfoSync()
const W = device.windowWidth
const H = device.windowHeight - 50
let cropper = require('../../utils/welCropper/welCropper.js');
var a = false
Page({

    /**
     * 页面的初始数据
     */
    data: {
        whether: 0,
        qualificationtext: '',
        qualificationphotos: [],
        infotype: 1,
        accountid: 0,
        psy_consult: {},
        factions_list: {},
        tag_list: {},
        tag_type_list: {},
        tags_time: {},
        record: {},
        avatar: '../res/0010.png',
        goodat_ids: {},
        qualification: 0,
        price: '',
        type_ids: {},
        factions_ids: {},
        ntype_ids: '',
        nfactions_ids: '',
        ngoodat_ids: '',
        nperiod_ids: '',
        intro: '',
        islow: 0,
        priceintro: '',
        forviewer: '',
        worktype: '',
        period_ids: {},
        arealist: {}, //省
        citylist: {}, //省
        provincename: '请选择省', //省
        cityname: '请选择市', //省
        province_id: 0,
        city_id: 0,
        name: '',
        items: [{
                name: '1',
                value: '是',
                checked: 'selected'
            },
            {
                name: '2',
                value: '否'
            },
        ],
    },
    qualificationInput: function(e) {
        this.setData({
            qualificationtext: e.detail.value
        })
    },
    mytouchstart: function(e) {
        this.setData({
            touch_start: e.timeStamp
        })
    },
    mytouchend: function(e) {
        this.setData({
            touch_end: e.timeStamp
        })
    },
    addimg: function() {
        a = true
        api.chooseimage(9, this.data.qualificationphotos, 'file', app.u.HOME_URL + app.u.UPLOADIMG).then(res => {
            this.setData({
                qualificationphotos: this.data.qualificationphotos.concat(res)
            })
        })
    },
    giftview: function(e) {
        a = true
        let touchTime = this.data.touch_end - this.data.touch_start
        if (touchTime > 350) {
            let that = this
            let images = that.data.qualificationphotos
            var index = e.currentTarget.dataset.index
            wx.showModal({
                title: '提示',
                content: '确定要删除此图片吗？',
                success(res) {
                    if (res.confirm) {
                        images.splice(index, 1)
                        that.setData({
                            qualificationphotos: images
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            wx.previewImage({
                current: e.currentTarget.dataset.url,
                urls: this.data.qualificationphotos
            })
        }
    },
    bindProvinceChange: function(e) {
        this.setData({
            province_id: this.data.arealist[e.detail.value].area_id,
            provincename: this.data.arealist[e.detail.value].area_name,
        })
        this.getarealist()
    },
    getarealist: function() {
        let url3 = app.u.HOME_URL + app.u.GET_AREA_LIST
        let data3 = { province: this.data.province_id }
        api.requestUrl(data3, url3).then(res => {
            if (res.code == 200) {
                this.setData({
                    city_id: res.data[0].area_id,
                    cityname: res.data[0].area_name
                })
                this.setData({
                    citylist: res.data
                })
            } else {
                api.showError(res.msg)
            }
        })
    },
    bindCityChange: function(e) {
        this.setData({
            city_id: this.data.citylist[e.detail.value].area_id,
            cityname: this.data.citylist[e.detail.value].area_name
        })
    },
    // bindPickerChange: function(e) {
    //     this.setData({
    //         qualification: this.data.tag_list[e.detail.value].id,
    //         qualificationtext: this.data.tag_list[e.detail.value].name
    //     })
    // },
    radioChange: function(e) {
        this.setData({
            whether: e.detail.value
        })
    },
    priceInput: function(e) {
        this.setData({
            price: e.detail.value
        })
    },
    period_idsChange: function(e) {
        this.setData({
            nperiod_ids: e.detail.value.join(',')
        })
    },
    type_idsChange: function(e) {
        this.setData({
            ntype_ids: e.detail.value.join(',')
        })
    },
    goodat_idsChange: function(e) {
        this.setData({
            ngoodat_ids: e.detail.value.join(',')
        })
    },
    factions_idsChange: function(e) {
        this.setData({
            nfactions_ids: e.detail.value.join(',')
        })
    },
    worktypeInput: function(e) {
        this.setData({
            worktype: e.detail.value
        })
    },
    priceinfoInput: function(e) {
        this.setData({
            priceintro: e.detail.value
        })
    },
    forviwerInput: function(e) {
        this.setData({
            forviewer: e.detail.value
        })
    },
    selectTap(e) {
        a = true
        let that = this
        let mode = e.currentTarget.dataset.mode

        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) {
                const tempFilePath = res.tempFilePaths[0]
                console.log(tempFilePath)

                // 将选取图片传入cropper，并显示cropper
                // mode=rectangle 返回图片path
                // mode=quadrangle 返回4个点的坐标，并不返回图片。这个模式需要配合后台使用，用于perspective correction
                // let modes = ["rectangle", "quadrangle"]
                // let mode = modes[1]   //rectangle, quadrangle
                that.showCropper({
                    src: tempFilePath,
                    mode: mode,
                    sizeType: ['original', 'compressed'], //'original'(default) | 'compressed'
                    maxLength: 1000, //默认2000，允许最大长宽，避免分辨率过大导致崩溃
                    callback: (res) => {
                        if (mode == 'rectangle') {
                            wx.uploadFile({
                                url: app.u.HOME_URL + app.u.UPLOADIMG, //仅为示例，非真实的接口地址
                                filePath: res,
                                name: 'file',
                                formData: {
                                    user: 'test'
                                },
                                success(res) {
                                    that.setData({
                                        avatar: res.data
                                    })

                                }
                            })
                            that.hideCropper()
                        } else {
                            wx.showModal({
                                title: '',
                                content: JSON.stringify(res),
                            })
                        }
                        // that.hideCropper() //隐藏，我在项目里是点击完成就上传，所以如果回调是上传，那么隐藏掉就行了，不用previewImage
                    }
                })
            }
        })
    },
    changeheadimg: function() {
        a = true
        let that = this
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.u.HOME_URL + app.u.UPLOADIMG, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        'user': 'test'
                    },
                    success(res) {
                        that.setData({
                                avatar: res.data
                            })
                            //do something
                    }
                })
            }
        })
    },
    updateinfo: function() {
        let that = this
        if (that.data.avatar === '../res/0010.png') {
            api.showError('请上传头像')
            return false
        }
        if (that.data.province_id === 0) {
            api.showError('请选择省份')
            return false
        }
        if (that.data.city_id === 0) {
            api.showError('请选择城市')
            return false
        }
        if (that.data.price === '0') {
            api.showError('请填写咨询费用')
            return false
        }
        if (that.data.qualificationtext === '') {
            api.showError('请填写资质')
            return false
        }
        if (that.data.qualificationphotos === '') {
            api.showError('请上传资质图片')
            return false
        }
        if (that.data.ntype_ids === '') {
            api.showError('请选择咨询方式')
            return false
        }
        if (that.data.nperiod_ids === '') {
            api.showError('请选择咨询时段')
            return false
        }
        if (that.data.ngoodat_ids === '') {
            api.showError('请选择擅长类型')
            return false
        }
        if (that.data.nfactions_ids === 0) {
            api.showError('请选择咨询流派')
            return false
        }

        let data = {
            consultid: that.data.record.id,
            avatar: that.data.avatar,
            qualification: that.data.qualification,
            price: that.data.price,
            type_ids: that.data.ntype_ids,
            factions_ids: that.data.nfactions_ids,
            period_ids: that.data.nperiod_ids,
            goodat_ids: that.data.ngoodat_ids,
            islow: that.data.islow,
            forviewer: that.data.forviewer,
            worktype: that.data.worktype,
            priceintro: that.data.priceintro,
            province_id: that.data.province_id,
            city_id: that.data.city_id,
            qualificationphotos: that.data.qualificationphotos.join(','),
            qualificationtext: that.data.qualificationtext
        }

        let url = app.u.HOME_URL + app.u.UPDATEINFO
        api.requestUrl(data, url).then(res => {
            if (res.code == 200) {
                api.showSuccess(res.msg)
                wx.navigateBack({
                    delta: 1,
                })
            } else {
                api.showError(res.msg)
            }
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        cropper.init.apply(that, [W, H]);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (a) {
            a = false;
            return;
        }
        if (this.isFromShare) return;
        let that = this
        let consultid = 0
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {
                if (res.data.consult) {
                    consultid = res.data.consult.id
                }
                let data = { id: consultid }
                let url = app.u.HOME_URL + app.u.PSY_TAGS
                api.requestUrl(data, url).then(res => {
                    if (res.code == 200) {
                        that.setData({
                            psy_consult: res.data.psy_consult,
                            factions_list: res.data.factions_list,
                            tag_list: res.data.tag_list,
                            tag_type_list: res.data.tag_type_list,
                            tags_time: res.data.tags_time,
                            record: res.data.record,
                        })
                        if (res.data.record) {
							if (res.data.record.qualificationtext) {
                                that.setData({
									qualificationtext: res.data.record.qualificationtext,
                                })
                            }
                            if (res.data.record.avatar) {
                                that.setData({
                                    avatar: res.data.record.avatar,
                                })
                            }
                            if (res.data.record.price) {
                                that.setData({
                                    price: res.data.record.price,
                                })
                            }
                            if (res.data.record.citylist) {
                                that.setData({
                                    citylist: res.data.record.citylist,
                                })
                            }
                            if (res.data.record.provincename) {
                                that.setData({
                                    provincename: res.data.record.provincename,
                                })
                            }
                            if (res.data.record.cityname) {
                                that.setData({
                                    cityname: res.data.record.cityname,
                                })
                            }
                            that.setData({
                                goodat_ids: res.data.record.goodat_ids,
                                qualification: res.data.record.qualification,
                                type_ids: res.data.record.type_ids,
                                factions_ids: res.data.record.factions_ids,
                                intro: res.data.record.intro,
                                islow: res.data.record.islow,
                                priceintro: res.data.record.priceintro,
                                forviewer: res.data.record.forviewer,
                                worktype: res.data.record.worktype,
                                period_ids: res.data.record.period_ids,
                                ntype_ids: res.data.record.ntype_ids,
                                nfactions_ids: res.data.record.nfactions_ids,
                                ngoodat_ids: res.data.record.ngoodat_ids,
                                nperiod_ids: res.data.record.nperiod_ids,
                                arealist: res.data.area,
                                province_id: res.data.record.province_id,
                                city_id: res.data.record.city_id,
                                name: res.data.record.name,
                                qualificationphotos: res.data.record.qualificationphotos
                            })
                        }

                    } else {
                        api.showError(res.msg)
                    }
                })
            }
        })
    },
})